import time

from selenium import webdriver

from CongfigFile.Varconfig import ChromePath
from until import find_element
from until.until import find_elements

wd = None
def open_browser(browser_name,*args):
    global wd
    '''
    open browser
    :param browser_name: 
    :param args: 
    :return: 
    '''
    if browser_name.lower() == 'chrome':
        wd = webdriver.Chrome(ChromePath)
    # elif browser_name.lower() == 'firefox':
    #     wd = webdriver.Firefox()

def get_url(url,*args):
    '''
    访问网址
    :param url:
    :param args:
    :return:
    '''
    try:
        wd.get(url)
    except Exception as e:
        print(e)

def max_window(*args):
    try:
        wd.maximize_window()
    except Exception as e:
        print(e)

def sleep(seconds,*args):
    time.sleep(seconds)

def switch_frame(location_type,location_express,*args):
    global wd
    try:
        i_frame = find_element(wd,location_type,location_express)
        wd.switch_to.frame(i_frame)
    except Exception as e:
        print(e)


def input_content(location_type,location_express,content,*args):
    global wd
    try:
        find_element(wd,location_type,location_express).send_keys(content)
    except Exception as e:
        print(e)

def click(location_type,location_express,*args):
    try:
        find_element(wd,location_type,location_express).click()
    except Exception as e:
        print(e)

def assert_title(except_value,*args):
    try:
        assert except_value in wd.title
    except Exception as e:
        print(e)

def close_browser(*args):

    try:
        wd.quit()
    except Exception as e:
        print(e)

def switch_default(*args):
    try:
        wd.switch_to.default_content()
    except Exception as e:
        print(e)

def assert_pagesource(assert_value,*args):
    try:
        assert assert_value in wd.page_source
    except Exception as e:
        print(e)

def input_subject(location_type,location_express,operate_value,*args):
    try:
        location_express,index = location_express.split(',')
        find_elements(wd,location_type,location_express)[int(index)].send_keys(operate_value)
    except Exception as e:
        print(e)


if __name__ == '__main__':
    open_browser('chrome')
    get_url('http://mail.163.com')
    max_window()
    sleep(2)
    switch_frame('tag name', 'iframe')
    input_content('name', 'email', '13037521553')
    input_content('name', 'password', 'Ruru970929.')
    click('id', 'dologin')
    sleep(2)
    assert_title('网易邮箱6.0版')
    close_browser()
